using System.Net;
using CampusModel;
using CampusModel.Enum;
using CampusModel.Model;
using Microsoft.EntityFrameworkCore;
using PublicProject.PublicClass.ExtensionException;

namespace CampusDataOperationLibrary;

public class AdminOperation(CampusModelDbContext dbContext)
{
    // Get all classes
    public async Task<List<Class>> GetClassesAsync()
    {
        return await dbContext.Classes.ToListAsync();
    }
    
    // Add a new class
    public async Task<bool> AddClassAsync(string className)
    {
        // Check if class already exists
        if (await dbContext.Classes.AnyAsync(c => c.ClassName == className))
        {
            throw new ExceptionWithStatus(HttpStatusCode.Conflict, "班级已存在");
        }
        
        var newClass = new Class
        {
            ClassName = className
        };
        
        dbContext.Classes.Add(newClass);
        return await dbContext.SaveChangesAsync() > 0;
    }
    
    // Update a class
    public async Task<bool> UpdateClassAsync(string oldClassName, string newClassName)
    {
        // Check if the new class name already exists (except for the current class)
        if (await dbContext.Classes.AnyAsync(c => c.ClassName == newClassName && c.ClassName != oldClassName))
        {
            throw new ExceptionWithStatus(HttpStatusCode.Conflict, "班级名称已存在");
        }
        
        var classToUpdate = await dbContext.Classes.FirstOrDefaultAsync(c => c.ClassName == oldClassName)
            ?? throw new ExceptionWithStatus(HttpStatusCode.NotFound, "班级不存在");
        
        classToUpdate.ClassName = newClassName;
        return await dbContext.SaveChangesAsync() > 0;
    }
    
    // Delete a class
    public async Task<bool> DeleteClassAsync(string className)
    {
        var classToDelete = await dbContext.Classes
            .Include(c => c.Students)
            .Include(c => c.Courses)
            .FirstOrDefaultAsync(c => c.ClassName == className)
            ?? throw new ExceptionWithStatus(HttpStatusCode.NotFound, "班级不存在");
        
        // Check if class has students
        if (classToDelete.Students.Any())
        {
            throw new ExceptionWithStatus(HttpStatusCode.Conflict, "班级中存在学生，无法删除");
        }
        
        // Check if class has courses
        if (classToDelete.Courses.Any())
        {
            throw new ExceptionWithStatus(HttpStatusCode.Conflict, "班级中存在课程，无法删除");
        }
        
        dbContext.Classes.Remove(classToDelete);
        return await dbContext.SaveChangesAsync() > 0;
    }
    
    // Get user by ID
    private async Task<User> GetUserByIdAsync(string id)
    {
        var userId = int.Parse(id);
        var user = await dbContext.Users.FindAsync(userId) 
            ?? throw new ExceptionWithStatus(HttpStatusCode.NotFound, "用户不存在");
        return user;
    }
    
    // Get admin by user
    private async Task<Admin> GetAdminAsync(User user)
    {
        if (user.Role != UserRole.Admin)
        {
            throw new ExceptionWithStatus(HttpStatusCode.Forbidden, "非管理员用户");
        }
        
        var admin = await dbContext.Admins.FindAsync(user.UserId) 
            ?? throw new ExceptionWithStatus(HttpStatusCode.NotFound, "管理员不存在");
        return admin;
    }
}
